<?php

namespace addons\qingdong\controller;

use addons\qingdong\model\Event;
use addons\qingdong\model\ExamineRecord;
use addons\qingdong\model\Message as MessageModel;
use addons\qingdong\model\Notice;
use addons\qingdong\model\ReceivablesPlan;
use addons\qingdong\model\Record;

/**
 * 通知接口
 */
class Message extends StaffApi
{
    protected $noNeedLogin = [];
    protected $noNeedRight = [];


    /**
     * 获取通知列表
     */
    public function getList()
    {
        $limit = input("limit/d", 10);

        $where = ['to_staff_id' => $this->auth->id ,'relation_type'=>['neq',MessageModel::EXAMINE_TYPE]];
        $records = MessageModel::where($where)->with(['fromStaff', 'examine'])->order('status asc,id desc')->paginate($limit);

        $this->success('请求成功', $records);
    }

    /**
     * 获取审批列表
     */
    public function getExamineRecord()
    {
        $limit = input("limit/d", 10);
        //合同审批，回款审批、业绩目标审批、办公审批 费用
        $relation_type = input('type');
        $status =  input('status',0);
        if($status == 1){
            $status = array('in','2,3');
        }
        $ids = ExamineRecord::where(['relation_type' => $relation_type,
            'status' => $status,
            'check_staff_id' => $this->auth->id])->column('id');
        $where = ['to_staff_id' => $this->auth->id, 'relation_id' => ['in', $ids], 'relation_type' => MessageModel::EXAMINE_TYPE];

        $records = MessageModel::where($where)->with(['fromStaff', 'examine'])->order('status asc,id desc')->paginate($limit);

        $this->success('请求成功', $records);
    }

    /**
     * 获取审批通知详情
     */
    public function getExamineInfo()
    {

        $data = [
            ExamineRecord::CONTRACT_TYPE => ['count' => 0, 'msg' => '暂无消息'],
            ExamineRecord::CONSUME_TYPE => ['count' => 0, 'msg' => '暂无消息'],
            ExamineRecord::RECEIVABLES_TYPE => ['count' => 0, 'msg' => '暂无消息'],
            ExamineRecord::APPROVAL_TYPE => ['count' => 0, 'msg' => '暂无消息'],
            ExamineRecord::ACHIEVEMENT_TYPE => ['count' => 0, 'msg' => '暂无消息'],
            ExamineRecord::CARD_TYPE => ['count' => 0, 'msg' => '暂无消息'],
            ExamineRecord::LEAVE_TYPE => ['count' => 0, 'msg' => '暂无消息'],
        ];
        foreach ($data as $type=>$v) {
            $ids = ExamineRecord::where(['status' => 0, 'check_staff_id' => $this->auth->id,
                'relation_type' => $type
            ])->column('id');
            $count = MessageModel::where(['to_staff_id' => $this->auth->id,'relation_id' => ['in', $ids],
                'relation_type' => MessageModel::EXAMINE_TYPE])
                ->count();
            if ($count) {
                $msg = MessageModel::where(['to_staff_id' => $this->auth->id, 'relation_id' => ['in', $ids],
                    'relation_type' => MessageModel::EXAMINE_TYPE])
                    ->order('id desc')
                    ->value('content');
                $data[$type] = ['count' => $count, 'msg' => $msg];
            }
        }

        $this->success('请求成功', $data);
    }

    /**
     * 获取通知详情
     */
    public function getInfo()
    {
        $leadsCount = MessageModel::where(['to_staff_id' => $this->auth->id,
            'relation_type' => ['neq', MessageModel::EXAMINE_TYPE], 'status' => 0])->count();
        if ($leadsCount > 0) {//其他提醒消息
            $leadsDetail = MessageModel::where([
                'relation_type' => ['neq', MessageModel::EXAMINE_TYPE],
                'to_staff_id' => $this->auth->id,
                'status' => 0
            ])->order('id desc')->find();
        }

        $noticeWhere['read_staff_ids'] = ['not like', "%,{$this->auth->id},%"];

        $noticeCount = Notice::where($noticeWhere)->count();
        if ($noticeCount > 0) {
            $noticeDetail = Notice::where(['read_staff_ids' => ['not like', "%,{$this->auth->id},%"]])->order('id desc')->find();
        }

        $eventOne = Event::where([
            'type' => 1,
            'status' => ['in', [0, 1]],
            'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
            'staff_id|owner_staff_id' => $this->auth->id
        ])->count();
        //合同审批
        $contract = ExamineRecord::where([
            'relation_type' => ExamineRecord::CONTRACT_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        //回款审批
        $receivables = ExamineRecord::where([
            'relation_type' => ExamineRecord::RECEIVABLES_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        //费用审批
        $consume = ExamineRecord::where([
            'relation_type' => ExamineRecord::CONSUME_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        //业绩目标审批
        $achievement = ExamineRecord::where([
            'relation_type' => ExamineRecord::ACHIEVEMENT_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        //办公审批
        $approval = ExamineRecord::where([
            'relation_type' => ExamineRecord::APPROVAL_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        //补卡审批
        $card = ExamineRecord::where([
            'relation_type' => ExamineRecord::CARD_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        //请假审批
        $leave = ExamineRecord::where([
            'relation_type' => ExamineRecord::LEAVE_TYPE,
            'status' => 0,
            'check_staff_id' => $this->auth->id
        ])->count();
        $receivablesPlan = ReceivablesPlan::where([
            'remind_date' => ['elt', date('Y-m-d')],
            'status' => 0,
            'owner_staff_id' => $this->auth->id
        ])->count();

        $eventsCount = Event::where([
            'start_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
            'end_time' => ['lt', date('Y-m-d', strtotime('+1 day'))],
            'status' => ['in', [0, 1]],
            'staff_id' => $this->auth->id,
        ])->count();

        $where['create_staff_id'] = $this->auth->id;
        $where['next_time'] = array(array('egt',date('Y-m-d 00:00:00')),array('lt',date('Y-m-d 23:59:59')));
        $where['follow_type'] = ['neq', '其它'];
        $where['status'] = 0;
        // 待跟进客户
        $where1['relation_type'] = 1;
        $customerlist = Record::where($where)->where($where1)->column('id');
        $customerlist1 = 0;
        if($customerlist){
            $whereExit['id'] = array('in',$customerlist);
            $whereExit['next_time'] = array('gt',date('Y-m-d 23:59:59'));
            $customerlist1 = Record::where($whereExit)->count();
        }
        $customer = count($customerlist) - $customerlist1;

        //待跟进合同
        $where2['relation_type'] = 3;
        $contractlist = Record::where($where)->where($where2)->column('id');
        $contractlist1 = 0;
        if($contractlist){
            $whereExitC['id'] = array('in',$contractlist);
            $whereExitC['next_time'] = array('gt',date('Y-m-d 23:59:59'));
            $contractlist1 = Record::where($whereExitC)->count();
        }
        $contracts = count($contractlist)-$contractlist1;
        //待跟进线索
        $where3['relation_type'] = 4;
        $leadlist = Record::where($where)->where($where3)->column('id');
        $leadlist1 = 0;
        if($leadlist){
            $whereExitL['id'] = array('in',$leadlist);
            $whereExitL['next_time'] = array('gt',date('Y-m-d 23:59:59'));
            $leadlist1 = Record::where($whereExitL)->count();
        }
        $lead = count($leadlist)-$leadlist1;
        //待跟进联系人
        $where4['relation_type'] = 2;
        $contactslist = Record::where($where)->where($where4)->column('id');
        $contactslist1 = 0;
        if($contactslist1){
            $whereExitCs['id'] = array('in',$contactslist);
            $whereExitCs['next_time'] = array('gt',date('Y-m-d 23:59:59'));
            $contactslist1 = Record::where($whereExitCs)->count();
        }
        $contacts = count($contactslist)-$contactslist1;
        //待跟进商机
        $where5['relation_type'] = 5;
        $businesslist = Record::where($where)->where($where5)->column('id');
        $businesslist1 = 0;
        if($businesslist1){
            $whereExitB['id'] = array('in',$businesslist);
            $whereExitB['next_time'] = array('gt',date('Y-m-d 23:59:59'));
            $businesslist1 = Record::where($whereExitB)->count();
        }
        $business = count($businesslist)-$businesslist1;
        //提醒消息
        $data = [
            'leads' => ['count' => $leadsCount, 'msg' => $leadsDetail['content'] ?? '暂无消息'],//提醒消息
            'examine' => ['count' => $contract + $receivables+$consume+$achievement+$approval+$card+$leave,
                        'msg' => '审批消息提醒'],//提醒消息
            'notice' => ['count' => $noticeCount, 'msg' => $noticeDetail['title'] ?? '暂无消息'],//系统公告
            'agent' => [
                'count' => $eventOne  + $receivablesPlan+ $customer + $contracts + $lead + $contacts + $business,
                'msg' => '您有新的待办事项需要处理！'
            ],//待办事项
            'task' => [
                'count' => $eventsCount,
                'msg' => '即将超时的任务提醒！'
            ],//超时任务
            'total' => $leadsCount + $noticeCount + $eventOne + $contract + $receivables + $receivablesPlan + $eventsCount+ $customer + $contracts + $lead + $contacts + $business+$consume+$achievement+$approval+$card+$leave,

            'userinfo' => $this->auth->getUserinfo()
        ];
        $this->success('请求成功', $data);
    }


}
